package com.dongge.dao;

import com.dongge.entity.Information;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

/**
 * Created by dongge on 16/7/31.
 */
@Repository
public class InformationDao {

    @Autowired
    private SessionFactory sessionFactory;

    protected Session getSession(){
        return  sessionFactory.getCurrentSession();
    }

    public Information get(long id){
        return (Information)getSession().get(Information.class,id);
    }

    public long save(Information information){
        return (Long)getSession().save(information);
    }

    public int update(Information information){
        Session session = getSession();
        String hql = "update Information info set info.content= :content"+
                "where info.id=:id";
        return session.createQuery(hql)
                .setString("content",information.getContent())
                .setLong("id",information.getId())
                .executeUpdate();
//        getSession().saveOrUpdate(information);
    }

    public List<Information> getInformations(){
        String hql = "from Information";
        return getSession().createQuery(hql).list();
    }

    /**
     * 返回受影响的行数
     * @param id
     * @return
     */
    public int delete(long id){
        return getSession().createQuery("delete from Information info where info.id = :id")
                .setLong("id",id).executeUpdate();
    }
}
